package com.bbx.taxi.client.Listener;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.SDK;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.util.LogUtils;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Message.Online;
import com.bbx.taxi.client.Service.LoginService;
import com.bbx.taxi.client.Service.TokenService;
import com.bbx.taxi.client.Util.IsServiceRunning;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class ConnectionChangeReceiver extends BroadcastReceiver {
	String packnameString = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		packnameString = context.getPackageName();

		ConnectivityManager connectMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobNetInfo = connectMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiNetInfo = connectMgr
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
			if(Value.isActive){
				ToastUtil.showToast(context, R.string.network_off);
			}
			LogUtils.e("bbx",context.getString(R.string.network_off));
			LoginService.SDKlogin=false;
			context.stopService(new Intent(context, LoginService.class));
		} else {
			// connect network
			// 启动登陆Service
			LogUtils.e("bbx",context.getString(R.string.network_on));
			 if(!IsServiceRunning.isServiceWork(context, "com.bbx.taxi.client.Service.LoginService")){
			// 启动请求TokenService
			   context.startService(new Intent(context, TokenService.class));	
			 }
		}
	}
}